package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.DzChewei;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.DzZufang;
import com.ruoyi.system.service.IDzZufangService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 租房Controller
 * 
 * @author ruoyi
 * @date 2023-08-17
 */
@RestController
@RequestMapping("/system/zufang")
public class DzZufangController extends BaseController
{
    @Autowired
    private IDzZufangService dzZufangService;

    /**
     * 查询租房列表
     */
    @GetMapping("/list")
    public TableDataInfo list(DzZufang dzZufang)
    {
        startPage();
        List<DzZufang> list = dzZufangService.selectDzZufangList(dzZufang);
        return getDataTable(list);
    }

    /**
     * 导出租房列表
     */
    @Log(title = "租房", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DzZufang dzZufang)
    {
        List<DzZufang> list = dzZufangService.selectDzZufangList(dzZufang);
        ExcelUtil<DzZufang> util = new ExcelUtil<DzZufang>(DzZufang.class);
        util.exportExcel(response, list, "租房数据");
    }


    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, Long xqId) throws Exception
    {
        ExcelUtil<DzZufang> util = new ExcelUtil<DzZufang>(DzZufang.class);
        List<DzZufang> list = util.importExcel(file.getInputStream());
        for(DzZufang lpfy:list){
            lpfy.setXqId(xqId);
            dzZufangService.insertDzZufang(lpfy);
        }
        return AjaxResult.success("导入成功");
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<DzZufang> util = new ExcelUtil<DzZufang>(DzZufang.class);
        util.importTemplateExcel(response, "数据");
    }

    /**
     * 获取租房详细信息
     */
    @GetMapping(value = "/{zfId}")
    public AjaxResult getInfo(@PathVariable("zfId") Long zfId)
    {
        return AjaxResult.success(dzZufangService.selectDzZufangByZfId(zfId));
    }

    /**
     * 新增租房
     */
    @Log(title = "租房", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DzZufang dzZufang)
    {
        return toAjax(dzZufangService.insertDzZufang(dzZufang));
    }

    /**
     * 修改租房
     */
    @Log(title = "租房", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DzZufang dzZufang)
    {
        return toAjax(dzZufangService.updateDzZufang(dzZufang));
    }

    /**
     * 删除租房
     */
    @Log(title = "租房", businessType = BusinessType.DELETE)
	@DeleteMapping("/{zfIds}")
    public AjaxResult remove(@PathVariable Long[] zfIds)
    {
        return toAjax(dzZufangService.deleteDzZufangByZfIds(zfIds));
    }
}
